package mmi.gui.widget;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.Box;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import mmi.file.WriteFile;
import mmi.gui.Application;
import mmi.gui.MainFrame;

import mmi.utils.QuestionAnswer;

public class QuestionnaireFrame extends JFrame {
	public enum Gender {MALE, FEMALE, UNKNOWN}
        private MainFrame parent;

	private class MultipleChoiceChooser extends JPanel
	{
		private static final long serialVersionUID = -8413717416489345092L;
		private JRadioButton button1 = new JRadioButton();
		private JRadioButton button2 = new JRadioButton();
		private JRadioButton button3 = new JRadioButton();
		private JRadioButton button4 = new JRadioButton();
		private JRadioButton button5 = new JRadioButton();

		public String question;

		public MultipleChoiceChooser(String text, String leftValueText, String rightValueText)
		{
			question = text;

			setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();

			ButtonGroup bg = new ButtonGroup();
			bg.add(button1);
			bg.add(button2);
			bg.add(button3);
			bg.add(button4);
			bg.add(button5);

			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.weightx = 0.0;
			gbc.gridwidth = 7;
			add(new JLabel(question), gbc);

			gbc.gridwidth = 1;

			gbc.gridx = 0;
			gbc.gridy = 1;
			add(new JLabel(leftValueText), gbc);

			gbc.gridx = 1;
			gbc.gridy = 1;
			add(button1, gbc);

			gbc.gridx = 2;
			gbc.gridy = 1;
			add(button2, gbc);

			gbc.gridx = 3;
			gbc.gridy = 1;
			add(button3, gbc);

			gbc.gridx = 4;
			gbc.gridy = 1;
			add(button4, gbc);

			gbc.gridx = 5;
			gbc.gridy = 1;
			add(button5, gbc);

			gbc.gridx = 6;
			gbc.gridy = 1;
			add(new JLabel(rightValueText), gbc);
		}

		public int getValue()
		{
			if(button1.isSelected()) {
				return 1;
			} else
				if(button2.isSelected()) {
					return 2;
				} else
					if(button3.isSelected()) {
						return 3;
					} else
						if(button4.isSelected()) {
							return 4;
						} else
							if(button5.isSelected()) {
								return 5;
							} else {
								return 0;
							}
		}
	};

        private class OpenQuestion extends JPanel
	{
		public String question;
                public JTextArea textArea;

		public OpenQuestion(String text)
		{
			question = text;

			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                        setPreferredSize(new Dimension(200, 100));

                        textArea = new JTextArea();
                        textArea.setEditable(true);

			add(new JLabel(question));
                        add(Box.createRigidArea(new Dimension(10,10)));
                        add(new JScrollPane(textArea));

                        pack();
                        setVisible(true);
		}

		public String getText()
		{
			return textArea.getText();
		}
	};

	private class PersonalInformationChooser extends JPanel
	{

		private static final long serialVersionUID = 2596440591743789769L;

		private JSpinner ageSpinner = new JSpinner();
		private JSpinner numberOfPrevExperimentsSpinner = new JSpinner();

		private JRadioButton maleButton = new JRadioButton();
		private JRadioButton femaleButton = new JRadioButton();
		public PersonalInformationChooser()
		{
        		setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();

                        ageSpinner.setPreferredSize(new Dimension(50, 20));
                        numberOfPrevExperimentsSpinner.setPreferredSize(new Dimension(50, 20));

			gbc.gridx = 0;
			gbc.gridy = 0;
			add(new JLabel("Age"), gbc);

			gbc.gridx = 1;
			gbc.gridy = 0;
			gbc.weightx = 0.0;
			gbc.gridwidth = 4;
			add(ageSpinner, gbc);


			ButtonGroup genderChooserGroup = new ButtonGroup();
			genderChooserGroup.add(maleButton);
			genderChooserGroup.add(femaleButton);


			gbc.gridwidth = 1;

			gbc.gridx = 0;
			gbc.gridy = 1;
			add(new JLabel("Gender"), gbc);

			gbc.gridx = 1;
			gbc.gridy = 1;
			add(new JLabel("M"), gbc);
			gbc.gridx = 2;
			gbc.gridy = 1;
			add(maleButton, gbc);

			gbc.gridx = 3;
			gbc.gridy = 1;
			add(new JLabel("F"), gbc);
			gbc.gridx = 4;
			gbc.gridy = 1;
			add(femaleButton, gbc);

			gbc.gridx = 0;
			gbc.gridy = 2;
			add(new JLabel("Experiments done so far"), gbc);
			gbc.gridx = 1;
			gbc.gridy = 2;
			gbc.weightx = 0.0;
			gbc.gridwidth = 4;
			add(numberOfPrevExperimentsSpinner, gbc);
		}
		public Integer getAge()
		{
			return (Integer) ageSpinner.getValue();
		}

		public Gender getGender()
		{
			if(maleButton.isSelected())
			{
				return Gender.MALE;
			} else
				if(femaleButton.isSelected())
				{
					return Gender.FEMALE;
				} else {
					return Gender.UNKNOWN;
				}
		}
	}

	private static final long serialVersionUID = 2205650801472563403L;

	private JPanel questionnairePanel = null;
	PersonalInformationChooser subjectPIC = null;
	MultipleChoiceChooser rectangleMCC = null;
	MultipleChoiceChooser ellipseMCC = null;
        MultipleChoiceChooser triangleMCC = null;
        MultipleChoiceChooser lineMCC = null;
        MultipleChoiceChooser freelineMCC = null;
        MultipleChoiceChooser textMCC = null;
        MultipleChoiceChooser linewidthMCC = null;
        MultipleChoiceChooser topMCC = null;
        MultipleChoiceChooser moveMCC = null;
        MultipleChoiceChooser resizeMCC = null;
        MultipleChoiceChooser rotateMCC = null;
        MultipleChoiceChooser fillMCC = null;
        MultipleChoiceChooser deleteMCC = null;
        MultipleChoiceChooser fillcolourMCC = null;
        MultipleChoiceChooser linecolourMCC = null;
        MultipleChoiceChooser colourpresetsMCC = null;
        MultipleChoiceChooser buttonlayoutMCC = null;
        OpenQuestion negativeOQ = null;
        OpenQuestion positiveOQ = null;
        JButton confirmButton = null;

	public QuestionnaireFrame(final MainFrame _parent)
	{
		super();
                parent = _parent;
		getContentPane().add(new JScrollPane(getQuestionnairePanel()));
                setPreferredSize(new Dimension(600, 1000));

		pack();
	}

	public Vector<QuestionAnswer> getAnswers()
	{
		Vector<QuestionAnswer> qa = new Vector<QuestionAnswer>();

		qa.add(new QuestionAnswer("Age", Integer.toString(subjectPIC.getAge())));
		qa.add(new QuestionAnswer("Gender", subjectPIC.getGender().toString()));
		qa.add(new QuestionAnswer(rectangleMCC.question, Integer.toString(rectangleMCC.getValue())));
		qa.add(new QuestionAnswer(ellipseMCC.question, Integer.toString(ellipseMCC.getValue())));
                qa.add(new QuestionAnswer(triangleMCC.question, Integer.toString(triangleMCC.getValue())));
                qa.add(new QuestionAnswer(lineMCC.question, Integer.toString(lineMCC.getValue())));
                qa.add(new QuestionAnswer(freelineMCC.question, Integer.toString(freelineMCC.getValue())));
                qa.add(new QuestionAnswer(textMCC.question, Integer.toString(textMCC.getValue())));
                qa.add(new QuestionAnswer(linewidthMCC.question, Integer.toString(linewidthMCC.getValue())));
                qa.add(new QuestionAnswer(topMCC.question, Integer.toString(topMCC.getValue())));
                qa.add(new QuestionAnswer(moveMCC.question, Integer.toString(moveMCC.getValue())));
                qa.add(new QuestionAnswer(resizeMCC.question, Integer.toString(resizeMCC.getValue())));
                qa.add(new QuestionAnswer(rotateMCC.question, Integer.toString(rotateMCC.getValue())));
                qa.add(new QuestionAnswer(fillMCC.question, Integer.toString(fillMCC.getValue())));
                qa.add(new QuestionAnswer(deleteMCC.question, Integer.toString(deleteMCC.getValue())));
                qa.add(new QuestionAnswer(fillcolourMCC.question, Integer.toString(fillcolourMCC.getValue())));
                qa.add(new QuestionAnswer(linecolourMCC.question, Integer.toString(linecolourMCC.getValue())));
                qa.add(new QuestionAnswer(colourpresetsMCC.question, Integer.toString(colourpresetsMCC.getValue())));
                qa.add(new QuestionAnswer(buttonlayoutMCC.question, Integer.toString(buttonlayoutMCC.getValue())));
                qa.add(new QuestionAnswer("Bad Points: ", negativeOQ.getText()));
                qa.add(new QuestionAnswer("Good Points: ", positiveOQ.getText()));
		return qa;
	}

	private JPanel getQuestionnairePanel()
	{
		if(questionnairePanel == null)
		{
			questionnairePanel = new JPanel();
			questionnairePanel.setLayout(new BoxLayout(questionnairePanel, BoxLayout.PAGE_AXIS));

                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));
                        
			subjectPIC = new PersonalInformationChooser();
			questionnairePanel.add(subjectPIC);

                        JLabel info = new JLabel("<html><center><br>Below are a number of questions regarding the usability of this program<br>" +
                                                  "Please rate the following functions (0-5)<br>" +
                                                  "If you did not use a function, please do not tick any of the boxes<br>" +
                                                  "<br></center></html>");
                        info.setAlignmentX(TOP_ALIGNMENT);
                        questionnairePanel.add(info);

			rectangleMCC = new MultipleChoiceChooser("Draw Rectangle", "Not at all easy", "Very easy");
			questionnairePanel.add(rectangleMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

			ellipseMCC = new MultipleChoiceChooser("Draw Ellipse", "Not at all easy", "Very easy");
			questionnairePanel.add(ellipseMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        triangleMCC = new MultipleChoiceChooser("Draw Triangle", "Not at all easy", "Very easy");
			questionnairePanel.add(triangleMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        lineMCC = new MultipleChoiceChooser("Draw Line", "Not at all easy", "Very easy");
			questionnairePanel.add(lineMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        freelineMCC = new MultipleChoiceChooser("Draw Free Line (pencil)", "Not at all easy", "Very easy");
			questionnairePanel.add(freelineMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        textMCC = new MultipleChoiceChooser("Add Text", "Not at all easy", "Very easy");
			questionnairePanel.add(textMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        linewidthMCC = new MultipleChoiceChooser("Changing Line width", "Not at all easy", "Very easy");
			questionnairePanel.add(linewidthMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        topMCC = new MultipleChoiceChooser("Top Tool", "Not at all easy", "Very easy");
			questionnairePanel.add(topMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        moveMCC = new MultipleChoiceChooser("Move Tool", "Not at all easy", "Very easy");
			questionnairePanel.add(moveMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        resizeMCC = new MultipleChoiceChooser("Resize Tool", "Not at all easy", "Very easy");
			questionnairePanel.add(resizeMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        rotateMCC = new MultipleChoiceChooser("Rotate Tool (line only)", "Not at all easy", "Very easy");
			questionnairePanel.add(rotateMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        fillMCC = new MultipleChoiceChooser("Fill Tool", "Not at all easy", "Very easy");
			questionnairePanel.add(fillMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        deleteMCC = new MultipleChoiceChooser("Delete Tool", "Not at all easy", "Very easy");
			questionnairePanel.add(deleteMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        fillcolourMCC = new MultipleChoiceChooser("Choosing Fill colour", "Not at all easy", "Very easy");
			questionnairePanel.add(fillcolourMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        linecolourMCC = new MultipleChoiceChooser("Choosing Line colour", "Not at all easy", "Very easy");
			questionnairePanel.add(linecolourMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        colourpresetsMCC = new MultipleChoiceChooser("Number of colour presets", "Very bad          ", "Very good");
			questionnairePanel.add(colourpresetsMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        buttonlayoutMCC = new MultipleChoiceChooser("Layout of buttons", "Very bad         ", "Very good");
			questionnairePanel.add(buttonlayoutMCC);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        negativeOQ = new OpenQuestion("Please tell us about the things you disliked(or liked the least)");
			questionnairePanel.add(negativeOQ);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        positiveOQ = new OpenQuestion("Please tell us about the things you liked the most(or disliked the least)");
			questionnairePanel.add(positiveOQ);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

                        confirmButton = new JButton("Confirm");
                        confirmButton.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                JFileChooser fileChooser = new JFileChooser();
                                fileChooser.setSelectedFile(new File("Questionnaire##"));
				int rv = fileChooser.showSaveDialog(QuestionnaireFrame.this);
				if (rv == JFileChooser.APPROVE_OPTION) {
                                    File file = fileChooser.getSelectedFile();
                                    new WriteFile(file, parent.getDrawingArea().getActionList(), getAnswers());
                                    Application.Trace("Saving: " + file.getName() + "." + "\n");
                                    setVisible(false);
                                }

                            }
                        });
                        questionnairePanel.add(confirmButton);
                        questionnairePanel.add(Box.createRigidArea(new Dimension(1,10)));

		}

		return questionnairePanel;
	}

	public void showFrame()
	{
		setVisible(true);
		repaint();
	}
}

